package com.jason.bbs.article.controller;

import java.util.Objects;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jason.bbs.article.fegin.BlockService;
import com.jason.bbs.article.fegin.UserService;
import com.jason.bbs.article.service.ArticleService;
import com.jason.bbs.bean.ResultJson;
import com.jason.bbs.bean.article.Article;

@RestController
public class ArticleController {
	@Resource
	private ArticleService service;
	@Resource
	private UserService userService;
	@Resource
	private BlockService blockService;

	@PutMapping("/add")
	public ResultJson add(@RequestBody Article article) {
		ResultJson code = new ResultJson(200, "成功");
		if (article == null) {
			code.setCode(400);
			code.setMessage("参数不能为空");
			return code;
		}
		if (article.getUserId() == null) {
			code.setCode(400);
			code.setMessage("文章作者编号不能为空");
			return code;
		}
		ResultJson rs = this.userService.exitis(article.getUserId());
		Object obj=rs.get("exitis");
		if(obj instanceof Boolean && (Boolean)obj == false) {
			code.setCode(400);
			code.setMessage("用户不存在");
			return code;
		}else if ("error".equals(obj)) {
			code.setCode(500);
			code.setMessage("用户模块暂时无法提供服务");
			return code;
		}
		if (article.getBlockId() == null) {
			code.setCode(400);
			code.setMessage("文章版块编号不能为空");
			return code;
		}
		rs = this.blockService.exitis(article.getBlockId());
		obj=rs.get("exitis");
		if(obj instanceof Boolean && (Boolean)obj == false) {
			code.setCode(400);
			code.setMessage("文章版块不存在");
			return code;
		}else if ("error".equals(obj)) {
			code.setCode(500);
			code.setMessage("版块模块暂时无法提供服务");
			return code;
		}
		if (StringUtils.isBlank(article.getTitle())) {
			code.setCode(400);
			code.setMessage("文章标题不能为空");
			return code;
		}
		if (StringUtils.isBlank(article.getContent())) {
			code.setCode(400);
			code.setMessage("文章内容不能为空");
			return code;
		}
		article = this.service.add(article);
		code.put("articleId", article.getId());
		return code;
	}

	@DeleteMapping("/delete/{articleId}")
	public ResultJson delete(@PathVariable("articleId") String id) {
		ResultJson code = new ResultJson(200, "成功");
		if (!id.matches("\\d+")) {
			code.setCode(400);
			code.setMessage("非法的文章编号:" + id);
			return code;
		}
		this.service.delete(id);
		return code;
	}

	@GetMapping("/find")
	public ResultJson find(Article article,
			@RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize,
			@RequestParam(name = "pageNumber", required = false, defaultValue = "1") int pageNumber) {
		Page<Article> page = this.service.find(article, pageNumber-1, pageSize);
		ResultJson code = new ResultJson(200, "成功");
		code.put("total", page.getTotalElements());
		code.put("totalPage", page.getTotalPages());
		code.put("pageNumber", page.getNumber() + 1);
		code.put("pageSize", page.getSize());
		code.put("list", page.getContent());
		return code;
	}

	@GetMapping("/exitis/{articleId}")
	public ResultJson exitis(@PathVariable("articleId") String articleId) {
		ResultJson code = new ResultJson(200, "成功");
		if (!articleId.matches("\\d+")) {
			code.setCode(400);
			code.setMessage("非法的文章编号:" + articleId);
			return code;
		}
		code.put("exitis", service.exitis(articleId));
		return code;
	}
}
